//
//  SetUpViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/17.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "SetUpViewController.h"
#import "CoreDataManager.h"
#import "City.h"
#import "CityViewController.h"
#import "EvationTableCell.h"

@interface SetUpViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CoreDataManager *manager;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) UILabel *notLabel;

@end

@implementation SetUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arr = [NSMutableArray array];
 
    [self creatData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
  
    // 设置navigationBar颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87/255.0 green:217/255.0 blue:200/255.0 alpha:1];
  
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"我的收藏";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = HEIGHT*220;
    self.tableView.tableFooterView = [UIView new];

    self.view.backgroundColor = [UIColor whiteColor];

    
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
}

- (void)creatData {
    // 查询数据库
    self.manager = [CoreDataManager shareCoreDataManager];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"City" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@",error);
    }
    [self.arr setArray:fetchedObjects];
    if (self.arr.count == 0) {
        [self createNotView];
    }
    else {
        [self createTableView];
        [self.tableView reloadData];
    }
}

- (void)createNotView {
    self.notLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * WIDTH, 200 * HEIGHT, 335 * WIDTH, 100 * HEIGHT)];
    self.notLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.notLabel];
    self.notLabel.font = [UIFont boldSystemFontOfSize:20];
    self.notLabel.text = @"您还没有任何收藏,请收藏了再来吧!";
    self.notLabel.numberOfLines = 0;
    self.notLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH*375, HEIGHT*667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = HEIGHT*220;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
}


// 返回
- (void)leftAction:(UIBarButtonItem *)barButtonItem {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor blackColor];
        self.notLabel.textColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor blackColor];
    }
    else {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87/255.0 green:217/255.0 blue:200/255.0 alpha:1];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.notLabel.textColor = [UIColor blackColor];
        self.view.backgroundColor = [UIColor whiteColor];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"cell";
    EvationTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[EvationTableCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    City *city = self.arr[indexPath.row];
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"F"]];
    cell.rightLabel.text = city.placeName;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    City *city = self.arr[indexPath.row];
    CityViewController *cVC = [[CityViewController alloc]init];
    cVC.cityId = city.cityId;
    cVC.isSelect = YES;
    [self.navigationController pushViewController:cVC animated:YES];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"shoucang" object:nil];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        City *city = self.arr[indexPath.row];
        [self.manager.managedObjectContext deleteObject:city];
        [self.manager saveContext];
        [self.arr removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
         [[NSNotificationCenter defaultCenter]postNotificationName:@"shoucang" object:nil];
        [self.tableView reloadData];

        if (self.arr.count == 0) {
            [self createNotView];
        }

    }
}
// 删除之后点返回按钮重新加载数据
- (void)viewWillAppear:(BOOL)animated {
    [self creatData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
